﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.OleDb;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using _3Layer_CuuHangDienMay.BusinessLogic;
using _3Layer_CuuHangDienMay.BusinessObject;

namespace _3Layer_CuuHangDienMay
{
    public partial class FormMain : Form
    {
        public static string connectString; //Chuoi ket noi

        public FormMain()
        {
            InitializeComponent();
        }

        private void FormMain_Load(object sender, EventArgs e)
        {
            txtStringConnect.Text = "Provider=SQLOLEDB;Data Source=MINH-PC\\SQLEXPRESS;Initial Catalog=CuaHangDienMay;User Id=sa;Password=123456;";
            EnableEditing(false);
            btThem.Enabled = false;
            btSua.Enabled = false;
            btXoa.Enabled = false;
        }

        private void btConnect_Click(object sender, EventArgs e)
        {
            connectString = txtStringConnect.Text;

            if (!LoadDataToGrid())
                return;

            LoadDataToComboBox();
            txtStringConnect.Enabled = false;
            btConnect.Enabled = false;
            btThem.Enabled = true;
            btSua.Enabled = true;
            btXoa.Enabled = true;
        }

        private bool LoadDataToGrid()
        {
            ProduceBUS produceBUS;
            try
            {
                produceBUS = new ProduceBUS();
            }
            catch (OleDbException ex)
            {
                MessageBox.Show(ex.Message);
                return false;
            }
            DataTable listProduces = produceBUS.GetListProduces("All");
            GridViewProduces.DataSource = listProduces;
            return true;
        }

        private void LoadDataToComboBox()
        {
            ItemProduceBUS itemProduceBUS = new ItemProduceBUS();
            List<string> listName;
            cbboxItemProduces.Items.Add("All");
            cbboxItemProduces.SelectedItem = "All";
            
            listName = itemProduceBUS.GetNameItemProduces();
            for (int i = 0; i < listName.Count; i++)
            {
                cbboxItemProduces.Items.Add(listName[i]);
            }
        }

        private void cbboxItemProduces_SelectedIndexChanged(object sender, EventArgs e)
        {
            DataTable listProduces = null;
            ProduceBUS produceBUS = new ProduceBUS();
            ItemProduceBUS itemProduceBUS = new ItemProduceBUS();
            string itemSelected = cbboxItemProduces.SelectedItem.ToString();

            if (itemSelected.Equals("All"))
            {
                listProduces = produceBUS.GetListProduces("All");
            }
            else
            {
                string sMaLoaiSanPham = itemProduceBUS.GetCodeItemProduces(itemSelected);
                if (sMaLoaiSanPham.Equals(""))
                {
                    return;
                }
                listProduces = produceBUS.GetListProduces(sMaLoaiSanPham);
            }
            GridViewProduces.DataSource = listProduces;
        }

        private void EnableEditing(bool editing)
        {
            // button
            btThem.Enabled = !editing;
            btSua.Enabled = !editing;
            btXoa.Enabled = !editing;
            btLuu.Enabled = editing;
            btKhongLuu.Enabled = editing;
            // textbox, combobox
            txtMaSanPham.Enabled = editing;
            txtTenSanPham.Enabled = editing;
            txtHangSanXuat.Enabled = editing;
            txtGiaBan.Enabled = editing;
            txtBaoHanh.Enabled = editing;
            txtXuatXu.Enabled = editing;
            txtSoLuongTon.Enabled = editing;
            txtLoaiSanPham.Enabled = editing;
        }
        private void ResetTextValue()
        {
            txtMaSanPham.Text = "";
            txtTenSanPham.Text = "";
            txtHangSanXuat.Text = "";
            txtGiaBan.Text = "";
            txtBaoHanh.Text = "";
            txtXuatXu.Text = "";
            txtSoLuongTon.Text = "";
            txtLoaiSanPham.Text = "";
        }

        private void btExit_Click(object sender, EventArgs e)
        {
            this.Dispose();
        }

        private void btThem_Click(object sender, EventArgs e)
        {
            EnableEditing(true);
            ResetTextValue();
        }

        private void btLuu_Click(object sender, EventArgs e)
        {
            Produce produce = GetInfoProduce();
            ProduceBUS produceBUS = new ProduceBUS();
            // Nếu tồn tại mã san pham => Sửa
            if (produceBUS.CheckExistsMASANPHAM(produce.MaSanPham))
            {
                if (produceBUS.UpdateProduce(produce))
                {
                    EnableEditing(false);
                    LoadDataToGrid();
                }
            }
            else // Thêm
            {
                if (produceBUS.AddProduce(produce))
                {
                    EnableEditing(false);
                    LoadDataToGrid();
                }
            }
        }

        private Produce GetInfoProduce()
        {
            Produce produce = new Produce();
            produce.MaSanPham = txtMaSanPham.Text;
            produce.TenSanPham = txtTenSanPham.Text;
            produce.HangSanXuat = txtHangSanXuat.Text;
            if (txtGiaBan.Text != "")
            {
                try
                {
                    produce.GiaBan = float.Parse(txtGiaBan.Text);  //try catch de tranh truong hop nhap vao la chuoi
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }        
            produce.BaoHanh = txtBaoHanh.Text;
            produce.XuatXu = txtXuatXu.Text;
            if (txtSoLuongTon.Text != "")
            {
                try
                {
                    produce.SoLuongTon = int.Parse(txtSoLuongTon.Text);  //try catch de tranh truong hop nhap vao la chuoi
                }
                catch (Exception e)
                {
                    MessageBox.Show(e.Message, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }  
            produce.MaLoaiSanPham = txtLoaiSanPham.Text;
            return produce;
        }

        private void btKhongLuu_Click(object sender, EventArgs e)
        {
            EnableEditing(false);
        }

        private void btSua_Click(object sender, EventArgs e)
        {
            EnableEditing(true);
            txtMaSanPham.Enabled = false;
        }

        private void GridViewProduces_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            // dòng hiện tại
            int indexRow = e.RowIndex;
            txtMaSanPham.Text = GridViewProduces.Rows[indexRow].Cells["MASANPHAM"].Value.ToString();
            txtTenSanPham.Text = GridViewProduces.Rows[indexRow].Cells["TENSANPHAM"].Value.ToString();
            txtHangSanXuat.Text = GridViewProduces.Rows[indexRow].Cells["HANGSANXUAT"].Value.ToString();
            txtGiaBan.Text = GridViewProduces.Rows[indexRow].Cells["GIABAN"].Value.ToString();
            txtBaoHanh.Text = GridViewProduces.Rows[indexRow].Cells["BAOHANH"].Value.ToString();
            txtXuatXu.Text = GridViewProduces.Rows[indexRow].Cells["XUATXU"].Value.ToString();
            txtSoLuongTon.Text = GridViewProduces.Rows[indexRow].Cells["SOLUONGTON"].Value.ToString();
            txtLoaiSanPham.Text = GridViewProduces.Rows[indexRow].Cells["MALOAISANPHAM"].Value.ToString();
        }

        private void btXoa_Click(object sender, EventArgs e)
        {
            ProduceBUS produceBUS = new ProduceBUS();
            //hien dialog hoi co muon xoa ko
            if (MessageBox.Show("Bạn có muốn xóa sản phẩm: " + txtTenSanPham.Text + " không?", "Hỏi",
                MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (produceBUS.DeleteProduce(txtMaSanPham.Text))
                {
                    LoadDataToGrid();
                }

            }
        }
    }
}
